import cv2 as cv
import matplotlib.pyplot as plt
# 读取图像
img = cv.imread("mao.png")  # 读取彩色图像 (BGR)

# 转换为RGB格式以适应matplotlib
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)

# 设置边界扩充参数
top = bottom = left = right = 50

# 不同的边界扩充方式
imgReplicate = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_REPLICATE)
imgReflect = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_REFLECT)
imgReflect101 = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_REFLECT_101)
imgWrap = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_WRAP)
imgConstant = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_CONSTANT, value=(200, 200, 200))

# 将边界扩充后的图像转换为RGB格式
imgReplicate_rgb = cv.cvtColor(imgReplicate, cv.COLOR_BGR2RGB)
imgReflect_rgb = cv.cvtColor(imgReflect, cv.COLOR_BGR2RGB)
imgReflect101_rgb = cv.cvtColor(imgReflect101, cv.COLOR_BGR2RGB)
imgWrap_rgb = cv.cvtColor(imgWrap, cv.COLOR_BGR2RGB)
imgConstant_rgb = cv.cvtColor(imgConstant, cv.COLOR_BGR2RGB)

# 创建一个窗口并显示图像
plt.figure(figsize=(18, 6))

plt.subplot(2, 3, 1)
plt.title('Original Image')
plt.imshow(img_rgb)
plt.axis('off')

plt.subplot(2, 3, 2)
plt.title('Replicate Border')
plt.imshow(imgReplicate_rgb)
plt.axis('on')

plt.subplot(2, 3, 3)
plt.title('Reflect Border')
plt.imshow(imgReflect_rgb)
plt.axis('on')

plt.subplot(2, 3, 4)
plt.title('Reflect101 Border')
plt.imshow(imgReflect101_rgb)
plt.axis('on')

plt.subplot(2, 3, 5)
plt.title('Wrap Border')
plt.imshow(imgWrap_rgb)
plt.axis('on')

plt.subplot(2, 3, 6)
plt.title('Constant Border')
plt.imshow(imgConstant_rgb)
plt.axis('on')

plt.tight_layout()
plt.show()